package com.bgds.sys.socket;


import cn.hutool.core.util.HexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;


/**
 * @author chenchao
 * @date 2021/10/21
 */
@Component
@Slf4j
public class SocketClient {

    @Value("${socket.host}")
    private String HOST;
    @Value("${socket.port}")
    private Integer PORT;


    /**
     * 发送消息
     *
     * @param bytes
     * @return void
     * @author chenchao
     * @date 2021/10/29
     */
    public void sendMessage(byte[] bytes) {
        Socket socket = null;
        DataOutputStream write = null;
        try {
            socket = new Socket(HOST, PORT);
            write = new DataOutputStream(socket.getOutputStream());
            write.write(bytes);
            write.flush();
        } catch (Exception e) {
            log.error("助燃风机socket电文发送异常:{}", e.getMessage());
        } finally {
            if (write != null) {
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    log.error("助燃风机socket关闭失败:{}", e.getMessage());
                }
            }
        }


    }

}
